var prefix = jsctx + "/tenantChargeLog";

var tenantChargeLog = (function () {
    function init() {
        load();
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listChargeProcessLog",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 10,
                    pageList: [10],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit),
                            operatorName: $("#operatorName").val(),
                            orderNum: $("#orderNum").val(),
                            nickName: $("#nickName").val(),
                            startTime: $("#startTime").val(),
                            endTime: $("#endTime").val(),
                            gunNo: $("#gunNo").val()
                        };
                    },
                    columns: [
                        {field: 'orderNum', title: '订单号', width: '100'},
                        {field: 'createTime', title: '通讯时间', width: '100', align: 'left'},
                        {field: 'gunNo', title: '充电枪编号', width: '100', align: 'left'},
                        {field: 'pileSort', title: '充电桩短号', width: '100', align: 'left'},
                        {field: 'stationName', title: '站点名称', width: '100', align: 'left'},
                        {field: 'operatorName', title: '所属加盟商', width: '100', align: 'left'},
                        {field: 'nickName', title: '用户名', width: '100', align: 'left'},
                        {field: 'carNumber', title: '车牌号', width: '100', align: 'left'},
                        {field: 'temperatureGun', title: '枪温度(°C)', width: '100', align: 'left'},
                        {field: 'gunPower', title: '功率', width: '100', align: 'left'},
                        {field: 'elec', title: '当前电流(A)', width: '100', align: 'left'},
                        {field: 'voltage', title: '当前电压(V)', width: '100', align: 'left'},
                        {field: 'demandElec', title: 'bms最大电流(A)', width: '100', align: 'left'},
                        {field: 'demandVoltage', title: 'bms最大电压(V)', width: '100', align: 'left'},
                        {field: 'temperatureGun', title: 'BMS温度(°C)', width: '100', align: 'left'},
                        {field: 'totalMoney', title: '金额', width: '100', align: 'left'},
                        {field: 'soc', title: 'soc(%)', width: '100', align: 'left'},
                        {
                            title: '操作',
                            field: '_id',
                            width: '200', align: 'center',
                            class: '',
                            formatter: function (value, row, index) {
                                var d = '<a class="btn btn-success btn-sm " href="#" title="详情" onclick="tenantChargeLog.detail(\''
                                    + row._id
                                    + '\',\''
                                    + row.createTime
                                    + '\')">详情</a> ';
                                return d;


                            }
                        }
                    ]
                });
    }
    /**
     * 导出列表
     */
    function exportList() {
        $.fileDownload(prefix + "/export", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                operatorName: $("#operatorName").val(),
                orderNum: $("#orderNum").val(),
                nickName: $("#nickName").val(),
                startTime: $("#startTime").val(),
                endTime: $("#endTime").val(),
                gunNo: $("#gunNo").val(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }


    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
        load();
    }


    function detail(id, startTime) {
        location.href = prefix + "/findById?id=" + id + "&startTime=" + startTime;
    }

    function deleteOperator(id) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/delete",
                type: "post",
                data: {
                    'id': id
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    function goBack() {
        window.history.back();
    }

    /*  模态框  */
    function showModel(id) {
        $('#exampleModal').modal('show');

        $("#submitButton").unbind("click").bind("click", function () {
            $.ajax({
                url: prefix + "/updateOperatorApplyForAudit",
                type: "post",
                data: {
                    "id": id,
                    "status": $("#status").val(),
                    "remark": $("#remark").val()
                },
                dataType: "json",
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                    }
                }
            })
        })

    }


    /*  模态框  */
    function showModel(id) {
        $('#exampleModal').modal('show');

        $("#submitButton").unbind("click").bind("click", function () {
            $.ajax({
                url: prefix + "/updateOperatorApplyForAudit",
                type: "post",
                data: {
                    "id": id,
                    "status": $("#status").val(),
                    "remark": $("#remark").val()
                },
                dataType: "json",
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                    }
                }
            })
        })

    }

    function showInvoiceModel(id) {
        $('#invoiceModal').modal('show');

        $("#submitInvoice").unbind("click").bind("click", function () {
            $.ajax({
                url: prefix + "/updateOperatorForInvoice",
                type: "post",
                data: {
                    "id": id,
                    "invoiceDateType": $("#invoiceDateType").val(),
                    "invoiceSingleMaxAmount": $("#invoiceSingleMaxAmount").val(),
                    "invoiceMaxAmount": $("#invoiceMaxAmount").val(),
                    "invoiceEachMaxGet": $("#invoiceEachMaxGet").val(),
                    "invoiceMaxGet": $("#invoiceMaxGet").val()
                },
                dataType: "json",
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#invoiceModal').on('hidden.bs.modal', function () {
                            document.getElementById("invoiceForm").reset();
                        })
                        $('#invoiceModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#invoiceModal').on('hidden.bs.modal', function () {
                            document.getElementById("invoiceForm").reset();
                        })
                        $('#invoiceModal').modal('hide')
                    }
                }
            })
        })

    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        showModel: showModel,
        detail: detail,
        delete: deleteOperator,
        exportList: exportList,
        showInvoiceModel: showInvoiceModel
    };
})();



